package cn.luowb.checkchat.config;

import cn.luowb.checkchat.common.context.IPContext;
import cn.luowb.checkchat.common.util.IPUtil;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Configuration;
import org.springframework.lang.NonNull;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * IP 配置, 用于拦截请求并获取用户 IP 地址
 */
@Configuration
@RequiredArgsConstructor
public class IpConfiguration implements WebMvcConfigurer {
    private final IPInterceptor ipInterceptor;

    @Override
    public void addInterceptors(@NonNull InterceptorRegistry registry) {
        registry.addInterceptor(ipInterceptor)
                .addPathPatterns("/**");
    }

    @Component
    static class IPInterceptor implements HandlerInterceptor {
        @Override
        public boolean preHandle(@NonNull HttpServletRequest request, @NonNull HttpServletResponse response,
                                 @NonNull Object handler) {
            String clientIP = IPUtil.getClientIP(request);
            IPContext.setIp(clientIP);
            return true;
        }

        @Override
        public void afterCompletion(@NonNull HttpServletRequest request, @NonNull HttpServletResponse response,
                                    @NonNull Object handler, @Nullable Exception ex) {
            IPContext.removeIp();
        }
    }
}
